package com.mes.web.controller.system;

import com.mes.common.constant.Constants;
import com.mes.common.core.domain.AjaxResult;
import com.mes.common.core.domain.entity.SysMenu;
import com.mes.common.core.domain.entity.SysUser;
import com.mes.common.core.domain.model.LoginBody;
import com.mes.common.core.redis.RedisCache;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.StringUtils;
import com.mes.framework.web.service.SysLoginService;
import com.mes.framework.web.service.SysPermissionService;
import com.mes.framework.web.service.TokenService;
import com.mes.system.domain.SysNotice;
import com.mes.system.domain.SysTenant;
import com.mes.system.service.SysMenuService;
import com.mes.system.service.SysNoticeService;
import com.mes.system.service.SysTenantService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class SysLoginController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private SysNoticeService sysNoticeService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysTenantService sysTenantService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getTenantName(),loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        //获取用户待读通知公告
        List<SysNotice> sysNoticeList = sysNoticeService.getNoticeListByUserId(user.getId());
        if (CollectionUtils.isEmpty(sysNoticeList)) {
            sysNoticeList = new ArrayList<SysNotice>();
        }
        String lincenseInfo = redisCache.getStringValue("mes.license.info");
        if (StringUtils.isNotEmpty(user.getTenantId())){
            SysTenant sysTenant = sysTenantService.get(user.getTenantId());
            user.setTenantName(sysTenant.getTenantName());
        }
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        ajax.put("lincenseInfo", lincenseInfo);
        ajax.put("sysNoticeList", sysNoticeList);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        // 用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
